package com.tripshare.sdk.weixin.converter;

import com.tripshare.sdk.weixin.api.util.JsonUtil;
import com.tripshare.sdk.weixin.api.util.XmlUtil;
import com.tripshare.sdk.weixin.format.WeixinBaseMsg;
import com.tripshare.sdk.weixin.format.receive.WeixinVoiceMsg;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

public class VoiceConverter implements Converter {

    /**
     * 明文消息反序列化
     *
     * @param msg
     * @return
     */
    public WeixinBaseMsg convert(String msg) throws IllegalAccessException, InvocationTargetException, InstantiationException {
        //反序列化
        Map<String, String> map = XmlUtil.xmlToObject(msg, Map.class);
        //文本消息
        return JsonUtil.gson.fromJson(JsonUtil.gson.toJson(map), WeixinVoiceMsg.class);
    }

    @Override
    public String convert(WeixinBaseMsg msg) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        //序列化
        return XmlUtil.objectToXml(msg);
    }
}
